<?php

$config = array (
    'IN' => "H:/parse/src",
    'OUT' => "H:/parse/out",
    'INITIALIZATION_ERROR' => "Error in initialization",
    'CURL_ERROR' => "Curl Error",
    'FILE_ERROR' => "File not found or is damaged",
    'LOG_ERROR' => "Warning: log file is not created",
    'WAIT_TIME' => 10
);

function get_curl($url, $file, $errMsg ){
    if (is_null($file))return $errMsg;
    $ch=curl_init($url );
    if($ch ){
	curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/2.0 (compatible; MSIE 2.1; Mac_PowerPC) ');
	curl_setopt($ch, CURLOPT_HEADER, 0 );
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10 );
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt ($ch, CURLOPT_FILE, $file);
  //  curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:9050' );  
	curl_exec($ch);
    $result = curl_getinfo($ch);
    curl_close($ch);
    return $result;
	}
    else return $errMsg;
}

function write_log($log, $str){
     if(!is_null($log)){
     $s = date('d.m.y H:i:s').'    '.$str.'\\n';
     fputs($log, $s);
     }
}

ini_set('max_execution_time','0');
ini_set('error_reporting', E_ALL);
ini_set('log_errors', '1');
ini_set('display_errors', '1');
$url_list = file($config['IN'].'/list1.txt');
$fileCounter = 0;
$logFilePath = $config['OUT'].'/log.txt';
$log = null;
$log = @fopen ($logFilePath, 'w');
if(!$log)echo $config['LOG_ERROR'].'<br/>';

foreach ($url_list as $i){
    $url=trim($i);
    $fileCounter++;
    $file = null;
    $file = @fopen($config['OUT'].'/'.$fileCounter.'.txt','w');
     write_log($log, 'Scanning    '.$url);
     $execResult = 'fail';
     $execResult = implode(",", get_curl($url, $file, $config['CURL_ERROR']));
     sleep($config['WAIT_TIME']);
     write_log($log, $execResult);
    if(!is_null($file) )fclose($file);
}
if(!is_null($log))fclose($log);
 ?>